// This file implements the DropDownMgr object

function DropDownMgr( doc )
{
    this.document = doc;
    this.curDDEl = null;
    this.curDD = null;
    this.fnOldBodyOnClick = null;
    this.aHideIds = null;
}

DropDownMgr.prototype.hide = function()
{
    var aAll = this.document.all;
    if( null != this.curDD ){
        if (this.curDD.close)
            this.curDD.close();
        else
            this.curDD.runtimeStyle.visibility = "";
        this.curDD = null;
        this.curDDEl = null;
        this.fnOldBodyOnClick = null;
        if( this.aHideIds ){
            for(var i=0; i<this.aHideIds.length; i++ ) {
                var oItem = aAll[this.aHideIds[i]];
                if (oItem) oItem.runtimeStyle.visibility = "";
            }
            this.aHideIds = null;
        }
    }
};

// The 2nd argument may either be an object or a string id of an element to show
DropDownMgr.prototype.show = function( el, oDDCtl, sFldId, sHideIds )
{
    var aAll = this.document.all;
    var bCtlObject = typeof oDDCtl == "object";
    var sDDId = bCtlObject ? oDDCtl.id : oDDCtl;
    var elDD  = aAll[sDDId];
    var fShow = true;
    var i;

    // use the object, if passed, since it might not have an id tag
    if (bCtlObject && !oDDCtl.id) elDD = oDDCtl;

    if( this.curDD ){
        if( this.curDDEl == el )
            fShow = false;
        this.hide();
    }

    if( fShow ){
        // If an array of flds is specified as to be "hidden", hide them
        if( arguments.length > 3 ){
            this.aHideIds = sHideIds.split(",");
            for( i=0; i<this.aHideIds.length; i++ ) {
                var oItem = aAll[this.aHideIds[i]];
                if (oItem) oItem.runtimeStyle.visibility = "hidden";
            }
        }

        if( bCtlObject && typeof oDDCtl.display != "undefined" ){
            var isObject = typeof sFldId == "object";
            var elFld = null;
            if (isObject) elFld = sFldId;
            else elFld = aAll[sFldId];
            if (elFld) oDDCtl.display( elFld );
        }
        else{
            var pos = getAbsPos( el );
            var iLeft = Math.max(4, (pos.x + el.offsetWidth - elDD.offsetWidth));
            elDD.runtimeStyle.pixelLeft = iLeft;
            elDD.runtimeStyle.pixelTop = pos.y + el.offsetHeight;
            elDD.runtimeStyle.visibility = "visible";
        }

        this.curDD = elDD;
        this.curDDEl = el;
        this.fnOldBodyOnClick = this.document.body.onclick;
        this.document.body.onclick = hideDropDownMgr;
    }
    cancelEvent();
};

function getDropDownMgr( doc )
{
    if (!window.ddMgr)
        window.ddMgr = new DropDownMgr( doc );
    return window.ddMgr;
}

function hideDropDownMgr()
{
    window.ddMgr.hide();
}

